// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace MatrixFramework.Photos.Domain.Entities;

/// <summary>
/// 照片人物实体
/// </summary>
public class PhotoPerson : FullAuditedEntity<Guid>, IMultiTenant
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public virtual Guid? TenantId { get; protected set; }

    /// <summary>
    /// 照片ID
    /// </summary>
    public virtual Guid PhotoId { get; protected set; }

    /// <summary>
    /// 人物ID
    /// </summary>
    public virtual Guid PersonId { get; protected set; }

    /// <summary>
    /// 人物姓名
    /// </summary>
    [MaxLength(100)]
    public virtual string? Name { get; protected set; }

    /// <summary>
    /// 人脸边界框（JSON格式）
    /// </summary>
    public virtual string? BoundingBox { get; protected set; }

    /// <summary>
    /// 置信度（0-1）
    /// </summary>
    public virtual double Confidence { get; protected set; }

    /// <summary>
    /// 年龄估计
    /// </summary>
    public virtual int? EstimatedAge { get; protected set; }

    /// <summary>
    /// 性别
    /// </summary>
    [MaxLength(10)]
    public virtual string? Gender { get; protected set; }

    /// <summary>
    /// 是否为主要人物
    /// </summary>
    public virtual bool IsMain { get; protected set; }

    /// <summary>
    /// 识别来源
    /// </summary>
    [MaxLength(100)]
    public virtual string? RecognitionSource { get; protected set; }

    /// <summary>
    /// 关联照片
    /// </summary>
    public virtual Photo Photo { get; protected set; } = null!;

    /// <summary>
    /// 关联人物
    /// </summary>
    public virtual Person Person { get; protected set; } = null!;

    #region 构造函数

    /// <summary>
    /// 参数化构造函数
    /// </summary>
    /// <param name="id">关系ID</param>
    /// <param name="tenantId">租户ID</param>
    /// <param name="photoId">照片ID</param>
    /// <param name="personId">人物ID</param>
    /// <param name="confidence">置信度</param>
    /// <param name="boundingBox">边界框</param>
    /// <param name="recognitionSource">识别来源</param>
    public PhotoPerson(
        Guid id,
        Guid? tenantId,
        Guid photoId,
        Guid personId,
        double confidence,
        string? boundingBox = null,
        string? recognitionSource = null) : base(id)
    {
        TenantId = tenantId;
        PhotoId = photoId;
        PersonId = personId;
        Confidence = Math.Clamp(confidence, 0.0, 1.0);
        BoundingBox = boundingBox;
        RecognitionSource = recognitionSource;
        IsMain = false;
    }

    /// <summary>
    /// 无参构造函数（用于EF Core）
    /// </summary>
    protected PhotoPerson()
    {
    }

    #endregion

    #region 业务方法

    /// <summary>
    /// 设置人物信息
    /// </summary>
    /// <param name="name">姓名</param>
    /// <param name="estimatedAge">估计年龄</param>
    /// <param name="gender">性别</param>
    public virtual void SetPersonInfo(string? name, int? estimatedAge, string? gender)
    {
        Name = name;
        EstimatedAge = estimatedAge;
        Gender = gender;
    }

    /// <summary>
    /// 更新置信度
    /// </summary>
    /// <param name="confidence">新置信度</param>
    public virtual void UpdateConfidence(double confidence)
    {
        Confidence = Math.Clamp(confidence, 0.0, 1.0);
    }

    /// <summary>
    /// 设置为主要人物
    /// </summary>
    /// <param name="isMain">是否主要</param>
    public virtual void SetMain(bool isMain)
    {
        IsMain = isMain;
    }

    /// <summary>
    /// 更新边界框
    /// </summary>
    /// <param name="boundingBox">新边界框</param>
    public virtual void UpdateBoundingBox(string boundingBox)
    {
        BoundingBox = boundingBox;
    }

    #endregion
}